package com.kborid.musicplayer;

import java.util.ArrayList;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.kborid.musicplayer.Control.ActivityBase;
import com.kborid.musicplayer.Control.MusicPlayControl;
import com.kborid.musicplayer.Control.MusicPlayControl.MusicState;
import com.kborid.musicplayer.Tools.FileUtilTool;
import com.kborid.musicplayer.Tools.MusicInfo;
import com.kborid.musicplayer.customLayout.RoundImageView;

public class ACT_Music_PlayList extends ActivityBase implements OnItemClickListener {

	private static final String TAG = "ACT_Music_PlayList";
	private ListView list;

	private MiniMusicAdapter adapter;
	private ArrayList<MusicInfo> playlist = null;
	private Bitmap bm = null;
	private ArrayList<Bitmap> bitmap = new ArrayList<Bitmap>();

	@Override
	protected void OnCreate(Bundle savedInstanceState) {
		super.OnCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.act_music_playlist);
		initialize();
		findViews();
		setListener();
		myHandler.post(mRunnable);
	}

	private void initialize() {
		playlist = MusicPlayControl.getInstance().getMusicPlayList();
		for (int i = 0; i < playlist.size(); i++) {
			bm = BitmapFactory.decodeResource(getResources(), R.drawable.p0134_menumusic_2slc);
			bitmap.add(bm);
		}
	}

	private void findViews() {
		list = (ListView) findViewById(R.id.list);

		adapter = new MiniMusicAdapter();
		list.setAdapter(adapter);
	}

	private void setListener() {
		list.setOnItemClickListener(this);
	}

	@Override
	protected void OnDestroy() {
		if (bm != null && !bm.isRecycled()) {
			bm.recycle();
			bm = null;
		}
		if (bitmap != null) {
			bitmap.clear();
			bitmap = null;
		}
		Log.i(TAG, "bitmap list = " + (bitmap == null));
		myHandler.removeCallbacks(mRunnable);
		Log.i(TAG, "mRunnable = " + mRunnable);
		super.OnDestroy();
	}

	@Override
	public boolean OnKeyDown(int keyCode, KeyEvent event) {
		finish();
		return super.OnKeyDown(keyCode, event);
	}

	public class MiniMusicAdapter extends BaseAdapter {

		public int getCount() {
			return playlist.size();
		}

		public Object getItem(int position) {
			return playlist.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder;

			if (convertView == null) {
				convertView = LayoutInflater.from(ACT_Music_PlayList.this).inflate(R.layout.musicplaylistitem, null);
				viewHolder = new ViewHolder();
				viewHolder.tv_id = (TextView) convertView.findViewById(R.id.tv_id);
				viewHolder.iv_icon = (RoundImageView) convertView.findViewById(R.id.iv_icon);
				viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
				viewHolder.tv_artist = (TextView) convertView.findViewById(R.id.tv_artist);
				viewHolder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);

				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}

			String id, title, artist, album, albumid, url, time;

			id = (position < 9) ? "0" + (position + 1) : "" + (position + 1);
			title = playlist.get(position).getTitle();
			artist = playlist.get(position).getArtist();
			album = playlist.get(position).getAlbum();
			albumid = playlist.get(position).getAlbumid();
			url = playlist.get(position).geturl();
			time = playlist.get(position).getDuration();

			viewHolder.iv_icon.setImageBitmap(bitmap.get(position));
			viewHolder.tv_id.setText(id);
			viewHolder.tv_title.setText(title + "[" + album + "]");
			viewHolder.tv_artist.setText(artist);
			long duration = Long.parseLong(time);
			viewHolder.tv_time.setText(FileUtilTool.long2timeFormat(duration));

			return convertView;
		}
	}

	static class ViewHolder {
		RoundImageView iv_icon;
		TextView tv_id;
		TextView tv_title;
		TextView tv_artist;
		TextView tv_time;
	}

	public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
		if (MusicPlayControl.getInstance().getCurrentIndex() != position || MusicPlayControl.getInstance().getPlayState() != MusicState.playing) {
			MusicPlayControl.getInstance().startPlayMusic(position);
		}
		finish();
	}

	public void notifyy() {
		adapter.notifyDataSetChanged();
	}

	private Handler myHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0x001:
				adapter.notifyDataSetChanged();
				break;
			default:
				break;
			}
		};
	};

	private Runnable mRunnable = new Runnable() {

		public void run() {
			for (int i = 0; i < playlist.size(); i++) {
				String albumid = playlist.get(i).getAlbumid();
				long albumlong = Long.parseLong(albumid);
				bitmap.set(i, FileUtilTool.getArtwork(ACT_Music_PlayList.this, i, albumlong, true, true));
				myHandler.sendEmptyMessage(0x001);
			}
		}
	};
}
